package apiTest;

import cn.com.elite.Util;
import cn.com.kh.hst.ThreeDesUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/* 4.1操作员统一登录授权接口 */
public class API_DevToken {
    private static final Log log = LogFactory.getLog(API_DevToken.class);

    public static void main(String[] args) throws Exception {
        String loginId = "guohebing";
        String password = "111111@@";
        String remoteAddr = "113.84.208.35";

        String m_ApiURL = "";

        //鉴权变量
        String appid = "40001";
        String appsecret = "zf7HxnNBg2o8fnCf611";

        //操作员密码，3DES加密传输
        String passwordEncrypted = ThreeDesUtil.encode(appsecret, password);
        String passwordEncrypted1 = URLEncoder.encode(passwordEncrypted, "utf-8");
        log.debug("解析信息-" + "passwordEncrypted1: " + passwordEncrypted1);

        String sign_time = String.valueOf(System.currentTimeMillis());
        log.debug("解析信息-" + "passwordEncrypted: " + passwordEncrypted + "sign_time: " + sign_time);


        HashMap<String, String> paramHashMap = new HashMap<String, String>() {
            {
                put("account", loginId);
                put("passwordEncrypted", passwordEncrypted);
                put("remoteAddr", remoteAddr);
                put("sign_time", sign_time);
            }
        };

        HashMap<String, String> paramHashMap1 = new HashMap<String, String>() {
            {
                put("account", loginId);
                put("passwordEncrypted", passwordEncrypted1);
                put("remoteAddr", remoteAddr);
                put("sign_time", sign_time);
            }
        };

        log.debug("paramHashMap：" + paramHashMap);
        //自然排序 升序参数
        String signfront = ThreeDesUtil.sortMap(paramHashMap, appid, appsecret);
        log.debug("sign加密MD5前：" + signfront);
        String signafter = Util.getMD5Str(signfront);
        log.debug("sign加密MD5后：" + signafter);

        //拼接接口入参
        paramHashMap1.put("client_key", appid);
        paramHashMap1.put("sign", signafter);

        String injson = ThreeDesUtil.SplicingMap(paramHashMap1, "&");
        log.debug("injson:" + injson);

        try {

            m_ApiURL = "https://cs-feature.mgbl.com.my" + "/passport/admin/login";

            Map<String, String> headMap = new HashMap<String, String>();
            headMap.put("Content-Type", "application/x-www-form-urlencoded");

            String getResult = Util.doHttpPostUseHead(m_ApiURL, injson, "UTF-8", 10000, 0, headMap);
            log.debug("CC系统登录信息表STAFF操作SQL结果:" + getResult);

        } catch (JSONException e) {
            log.error("接口处理异常" + e.getMessage());

        }


    }
}